<template>
  <j-modal
    :confirmLoading="confirmLoading"
    :dialogStyle="dialogStyle"
    :title="title"
    :visible="visible"
    :width="1200"
    cancelText="关闭"
    switchFullscreen
    @cancel="handleCancel"
    @ok="handleOk">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row justify="space-around" type="flex">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="年份">
              <a-select v-decorator="['year', {
            rules: [
              {
                required: true,
                message: '年份不能为空!',
              },
            ],
            }]" placeholder="请选择年份">
                <a-select-option v-for="(i,index) in yearList" :key="index" :value="i.key">{{ i.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="11" style="display: flex;">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="类别" style="flex: 1">
              <a-select v-decorator="['typeName', {
            rules: [
              {
                required: true,
                message: '类别不能为空!',
              },
            ],
            }]" mode="multiple" placeholder="请选择类别" style="width: 110%" @change="handleChange">
                <a-select-option v-for="(i,index) in selectList" :key="index" :value="i.key">{{ i.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
            <a-button v-if="onSelectItems !== allselectSample" icon="plus-circle" style="margin-top: 4px"
                      @click="allselect(true)">快捷全选
            </a-button>
            <a-button v-else-if="onSelectItems == allselectSample" icon="stop" style="margin-top: 4px"
                      @click="allselect(false)">取消全选
            </a-button>
          </a-col>
        </a-row>
        <a-row v-for="(i,index) in onSelectItems" :key="index" justify="space-around" type="flex">
          <a-divider>{{ i }}</a-divider>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="煤品">
              <a-input v-decorator="[`type[${index}].coalProducts`, {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入煤品量" suffix="万吨标准煤" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="油品">
              <a-input v-decorator="[`type[${index}].oils`, {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入油品量" suffix="万吨标准煤" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="天然气">
              <a-input v-decorator="[`type[${index}].naturalGas`, {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入天然气量" suffix="万吨标准煤" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="热力">
              <a-input v-decorator="[`type[${index}].heatingPower`, {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入热力量" suffix="万吨标准煤" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="电力">
              <a-input v-decorator="[`type[${index}].power`, {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入电力量" suffix="亿千瓦时" />
            </a-form-item>
          </a-col>
          <a-col :span="12">

          </a-col>
        </a-row>


      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import pick from 'lodash.pick'
import { httpAction, postAction } from '@api/manage'

const date = new Date()

export default {
  name: 'EngConsumptionModal',
  data() {
    return {
      confirmLoading: false,
      title: '',
      visible: false,
      form: this.$form.createForm(this),
      currentTime: '',
      dialogStyle: {
        top: '100px'
        // right:"20px",
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      selectList: [
        { name: '能源电力', key: '能源电力' },
        { name: '农业', key: '农业' },
        { name: '工业', key: '工业' },
        { name: '钢铁', key: '钢铁' },
        { name: '石化化工', key: '石化化工' },
        { name: '工业其他', key: '工业其他' },
        { name: '建筑业', key: '建筑业' },
        { name: '交通', key: '交通' }
      ],
      onSelectItems: [],
      yearList: [
        { name: date.getFullYear(), key: date.getFullYear() },
        { name: date.getFullYear() - 1, key: date.getFullYear() - 1 },
        { name: date.getFullYear() - 2, key: date.getFullYear() - 2 },
        { name: date.getFullYear() - 3, key: date.getFullYear() - 3 }

      ],
      url: {
        add: '/carbon/energyAnalysisConsumptionList/add',
        edit: '/carbon/energyAnalysisConsumptionList/edit'
      },
      allselectSample: [
        '能源电力',
        '农业',
        '工业',
        '钢铁',
        '石化化工',
        '工业其他',
        '建筑业',
        '交通'
      ]
    }
  },
  // 监听this.form.getFieldValue('typeName')的变化

  methods: {
    allselect(status) {
      if (status) {
        this.onSelectItems = this.allselectSample
        this.form.setFieldsValue({
          typeName: this.onSelectItems
        })
      } else {
        this.onSelectItems = []
        this.form.setFieldsValue({
          typeName: this.onSelectItems
        })
      }

    },
    add() {
      this.edit({})
    },
    edit(record) {
      console.log(record)
      this.visible = true
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(
          this.model,
          'year', 'typeName', 'coalProducts', 'oils', 'naturalGas', 'heatingPower', 'power'
        ))
      })
    },
    handleChange(value) {
      console.log(value)
      this.onSelectItems = value
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    async handleOk() {
      let that = this
      this.form.validateFields(async (err, values) => {
        if (!err) {
          let httpurl = ''
          let method = ''
          this.confirmLoading = true
          console.log(this.model)
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          console.log(httpurl)
          let formData = Object.assign(this.model, values)
          console.log(formData.type.length)
          let loopTime = formData.type.length
          for (let i = 0; i < loopTime; i++) {
            formData.coalProducts = formData.type[i].coalProducts
            formData.oils = formData.type[i].oils
            formData.naturalGas = formData.type[i].naturalGas
            formData.heatingPower = formData.type[i].heatingPower
            formData.power = formData.type[i].power
            formData.typeName = this.onSelectItems[i]

            try {
              const res = await httpAction(httpurl, formData, method);
              if (!res.success) {
                this.$message.error(res.message);
                break; // 如果请求失败，退出循环
              }else{
                if (i == loopTime - 1) {
                  that.confirmLoading = false
                  that.close()
                  that.$parent.$parent.loadData()
                }
              }
            } catch (error) {
              this.$message.error("请求失败，请稍后重试！");
              break; // 如果请求失败，退出循环
            }
          }
        }
      })
    }
  }
}
</script>

<style scoped>

</style>